package com.hp.service;

import com.hp.dao.AgentApplyMapper;
import com.hp.dao.PhotoMapper;
import com.hp.dao.UserMapper;
import com.hp.pojo.AgentApply;
import com.hp.pojo.Photo;
import com.hp.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author cksh
 * @create 2021-07-08 21:33
 */
@Service
public class FileService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    AgentApplyMapper agentApplyMapper;

    @Autowired
    private PhotoMapper photoMapper;

    private final String baseUrl = "http://192.168.43.54:8002/";

    public Map<String, Object> uploadHeadPhoto(MultipartFile file, String filePath, int userId) {

        HashMap<String, Object> map = new HashMap<>();

        if (file == null || file.isEmpty()) {
            map.put("msg", "未选择需上传的文件！");
            return map;
        }

        File fileUpload = new File(filePath);
        if (!fileUpload.exists()) {
            fileUpload.mkdirs();
        }

        fileUpload = new File(filePath, file.getOriginalFilename());
        try {
            file.transferTo(fileUpload);
            map.put("msg", "上传文件到服务器成功！");
            User user = userMapper.getUserById(userId);
            HashMap<String, Object> message = new HashMap<>();
            message.put("userId", user.getUserId());
            message.put("userPhoto", "http://192.168.43.54:8002/images/user/" + userId + "/" + file.getOriginalFilename());
            userMapper.updateUserById(message);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("msg", "上传文件到服务器失败！");
        }

        return map;
    }

    public Map<String, Object> uploadApplyPhoto(MultipartFile file, String filePath, int applyId) {
        HashMap<String, Object> map = new HashMap<>();

        if (file == null || file.isEmpty()) {
            map.put("msg", "未选择需上传的文件！");
            return map;
        }

        File fileUpload = new File(filePath);
        if (!fileUpload.exists()) {
            fileUpload.mkdirs();
        }
        fileUpload = new File(filePath, file.getOriginalFilename());
        try {
            file.transferTo(fileUpload);
            map.put("msg", "上传文件到服务器成功！");
            AgentApply agentApply = agentApplyMapper.getAgentByAgentId(applyId);
            HashMap<String, Object> message = new HashMap<>();
            message.put("applyId", agentApply.getApplyId());
            message.put("userPhoto", "http://192.168.43.54:8002/images/apply/" + applyId + "/" + file.getOriginalFilename());
            agentApplyMapper.updateAgentApply(message);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("msg", "上传文件到服务器失败！");
        }
        return map;
    }

    public Map<String, Object> uploadHousePhoto(MultipartFile file, String filePath, int houseId) {

        HashMap<String, Object> map = new HashMap<>();

        if (file == null || file.isEmpty()) {
            map.put("msg", "未选择需上传的文件！");
            return map;
        }

        File fileUpload = new File(filePath);
        if (!fileUpload.exists()) {
            fileUpload.mkdirs();
        }

        fileUpload = new File(filePath, file.getOriginalFilename());
        try {
            file.transferTo(fileUpload);
            map.put("msg", "上传文件到服务器成功！");
            Photo photo = new Photo();
            photo.setHouseId(houseId);
            photo.setPhotoLocation(baseUrl + "images/house/" + houseId + "/" + file.getOriginalFilename());
            photoMapper.addPhoto(photo);
        } catch (IOException e) {
            e.printStackTrace();
            map.put("msg", "上传文件到服务器失败！");
        }

        return map;
    }

    public Map<String, Object> uploadFile(MultipartFile file, String filePath, String netPath) {

        HashMap<String, Object> map = new HashMap<>();

        if (file == null || file.isEmpty()) {
            map.put("msg", "未选择需上传的文件！");
            return map;
        }

        File fileUpload = new File(filePath);
        if (!fileUpload.exists()) {
            fileUpload.mkdirs();
        }

        fileUpload = new File(filePath, file.getOriginalFilename());
        try {
            file.transferTo(fileUpload);
            map.put("msg", "上传文件到服务器成功！");
            map.put("status", 1);
            map.put("filePath", netPath + file.getOriginalFilename());
        } catch (IOException e) {
            e.printStackTrace();
            map.put("msg", "上传文件到服务器失败！");
            map.put("status", -1);
        }

        return map;
    }

    public Map<String, Object> uploadVoice(MultipartFile file, String filePath, String netPath) {
        return uploadFile(file, filePath, netPath);
    }

    public Map<String, Object> uploadNewsImg(MultipartFile img, String s, String netPath) {
        return uploadFile(img, s, netPath);
    }
}
